package com.inspur.gs.sbssdl.sbwhgl.sbrh.sbrh.back.commoncmps;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.caf.db.dbaccess.DynamicResultRow;
import com.inspur.edp.dxc.spi.components.BaseCustomValidateComponent;
import com.inspur.edp.qdp.bql.api.IBqlExecuter;
import com.inspur.edp.qdp.bql.api.OptionType;
import com.inspur.gs.sbssdl.sbwhgl.sbrh.sbrh.back.luCreatFactory.LuCreat;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.runtime.config.BqlBeanUtil;
import io.iec.edp.caf.sysmanager.api.manager.org.SysOrgManager;
import io.iec.edp.caf.sysmanager.api.manager.user.UserManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class SbssSbrhTaskScheduler extends BaseCustomValidateComponent {

    private static final Logger log = LoggerFactory.getLogger(SbssSbrhTaskScheduler.class);
    private final IBqlExecuter bqlExecuter = BqlBeanUtil.getAppCtx().getBean(IBqlExecuter.class);

    public SbssSbrhTaskScheduler() {
    }

    public void checkTodayTasks() {
        String sql = "select id as id from SBSS_SBTZ_SBTZ where rh = 1";
        bqlExecuter.getOptions().setOptionType(OptionType.ExecuteSql);

        List<DynamicResultRow> rows = bqlExecuter.executeSelectStatement(sql, new ArrayList<>());
        List<String> id = rows.stream().map(i -> i.get("id").toString()).collect(Collectors.toList());

        SpringBeanUtils.getBean(IBefSessionManager.class).createSession(Duration.ofHours(2));
        try {
            // 查询计划，遍历注入LuCreat
            LuCreat luCreat = new LuCreat(id);
            luCreat.setScheduler(true);
            luCreat.creatLu();
        } finally {
            SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
        }

    }


    private void SyncOrg() {
        ObjectMapper objectMapper = new ObjectMapper();
        UserManager userManager = SpringBeanUtils.getBean(UserManager.class);
        SysOrgManager sysOrgManager = SpringBeanUtils.getBean(SysOrgManager.class);
    }
}